package ControlLayer;
import java.util.*;
import ModelLayer.*;

public class LendingsCtr
{
    private LendCon lendingCon; //reference to container class
    private static LendingsCtr instance; //Singleton
    private AddressCtr addrCtr; //reference to other controller classes 
    private DVDCtr dvdCtr;
    
    /**
     * Constructor for objects of class LendingsCtr
     */
    public LendingsCtr()
    {
       lendingCon = lendingCon.getInstance();
        addrCtr = addrCtr.getInstance();
       dvdCtr = dvdCtr.getInstance();
    }
    
    /**
     * Singleton method for working with only one instance of LendingsCtr.
     * (not strictly necessary for controller classes (as it is for containerclasses) but might still be convenient.)
     */
    public static LendingsCtr getInstance()
    {
        if(instance == null)
        {
            instance = new LendingsCtr();
        }
        return instance;
    }
    
    /**
    * prints a list of all persons registered in the system
    */
    public void listAllPersons()
    {
        addrCtr.listAllPersons();
    }
    
    /**
    * prints a list of all dvds registered in the system
    */
    public void listAllDvds()
    {
        dvdCtr.listAllDvds();
    }
    
    /**
    * returns a list of all loans registered in the system
    */
    public void listAllLendings()
    {
        lendingCon.printAllLendings();       
    }
    
    public Lendings getLending(long id)
    {
        return lendingCon.getLending(id);
    }
    
    /**
     * Returns an object of type Person from the container class
     */
    public Person getPerson(long id)
    {
        return addrCtr.getPerson(id);
    }
    
    /**
     * Returns an object of type DVD from the container class
     */
    public DVD getDVD(long id)
    {
        return dvdCtr.getDVD(id);
    }
    
    /**
     * sweeps through every instance of copy in every instance dvd in containerclass.
     * every copy is checked for boolean isOut. Returns list of copies where (isOut = true)
     */
    public void lendsOut()
    {
        /*      method not coded yet
         *          sweeps through
         * 
         *      sweep igennem hver enkelt dvd i dvdCon. 
         *      For hver dvd sweepes der igennem copies.
         *      For hver copy tjekkes boolean isOut.
         *      Hvis (isOut == true) laves print af instansen.
         */
    }
    
    /**
     * creates a new lend in the container if there are remaining copies available to loan.
     */
    public void  createLend(long pId, long dvdId, String period, String date)
    {
        Person p = addrCtr.getPerson(pId);
        DVD dvd = dvdCtr.getDVD(dvdId);
        ArrayList<Copy> copies = dvd.getAllCopies();
        
        boolean found = false;
        int i= 0;
        Copy c = null;
        while(i < copies.size() && !found)
        {
            c = copies.get(i);
            if(!lendingCon.isOut(c)) {
                found = true;
            }
            else {
                i++;
            }
            
        }
 
        if(found)
        {
            lendingCon.createLending(p, c, date, period);
        }
        
        else {
            System.out.println("No avaiable copies");
        }
    }
    
    /**
     * removes an instance from the list of lendings.
     */
    public void returnDVDSerialNr(long serialNr)
    {
        lendingCon.listAllLendings().remove(lendingCon.getLendingSerialNr(serialNr));
    }
    
    /**
     * removes an instance from the list of lendings.
     */
    public void returnDVDLendingId(long id)
    {
        lendingCon.listAllLendings().remove(lendingCon.getLending(id));
    }
}

